Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
4 / 6
CRAP
80.00% covered (warning)
80.00%
24 / 30
ActionMetadataProvider
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
4 / 6
15.57
80.00% covered (warning)
80.00%
24 / 30
 __construct
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 isKnownAction
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getActions
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 warmUpCache
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 clearCache
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 4
 ensureMetadataLoaded
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
15 / 15
<?php
namespace Oro\Bundle\SecurityBundle\Metadata;
use Doctrine\Common\Cache\CacheProvider;
class ActionMetadataProvider
{
    const CACHE_NAMESPACE = 'AclAction';
    const CACHE_KEY = 'data';
    /**
     * @var AclAnnotationProvider
     */
    protected $annotationProvider;
    /**
     * @var CacheProvider
     */
    protected $cache;
    /**
     * @var array
     *         key = action name
     *         value = ActionMetadata
     */
    protected $localCache;
    /**
     * Constructor
     *
     * @param AclAnnotationProvider $annotationProvider
     * @param CacheProvider|null    $cache
     */
    public function __construct(
        AclAnnotationProvider $annotationProvider,
        CacheProvider $cache = null
    ) {
        $this->annotationProvider = $annotationProvider;
        $this->cache = $cache;
        if ($this->cache !== null && $this->cache->getNamespace() === '') {
            $this->cache->setNamespace(self::CACHE_NAMESPACE);
        }
    }
    /**
     * Checks whether an action with the given name is defined.
     *
     * @param  string $actionName The entity class name
     * @return bool
     */
    public function isKnownAction($actionName)
    {
        $this->ensureMetadataLoaded();
        return isset($this->localCache[$actionName]);
    }
    /**
     * Gets metadata for all actions.
     *
     * @return ActionMetadata[]
     */
    public function getActions()
    {
        $this->ensureMetadataLoaded();
        return array_values($this->localCache);
    }
    /**
     * Warms up the cache
     */
    public function warmUpCache()
    {
        $this->ensureMetadataLoaded();
    }
    /**
     * Clears the cache
     */
    public function clearCache()
    {
        if ($this->cache) {
            $this->cache->delete(self::CACHE_KEY);
        }
        $this->localCache = null;
    }
    /**
     * Makes sure that metadata are loaded
     */
    protected function ensureMetadataLoaded()
    {
        if ($this->localCache === null) {
            $data = null;
            if ($this->cache) {
                $data = $this->cache->fetch(self::CACHE_KEY);
            }
            if (!$data) {
                $data = [];
                foreach ($this->annotationProvider->getAnnotations('action') as $annotation) {
                    $data[$annotation->getId()] = new ActionMetadata(
                        $annotation->getId(),
                        $annotation->getGroup(),
                        $annotation->getLabel()
                    );
                }
                if ($this->cache) {
                    $this->cache->save(self::CACHE_KEY, $data);
                }
            }
            $this->localCache = $data;
        }
    }
}